package cn.bjit.service.impl;

import cn.bjit.interceptor.LoginInterceptor;
import cn.bjit.mapper.AddressMapper;
import cn.bjit.model.LoginUser;
import cn.bjit.model.AddressDO;
import cn.bjit.request.AddressAddRequest;
import cn.bjit.service.AddressService;
import cn.bjit.vo.AddressVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AddressServiceImpl implements AddressService {
   @Resource
    private AddressMapper addressMapper;


    /**
     * 添加收货地址
     * @param addressAddRequest
     */
    @Override
    public void add(AddressAddRequest addressAddRequest) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        AddressDO addressDO=new AddressDO();

        addressDO.setCreateTime(new Date());
        addressDO.setUserId(loginUser.getId());

        BeanUtils.copyProperties(addressAddRequest,addressDO);

        //>>1，是否有默认的收货地址
        if(addressDO.getDefaultStatus()==1){  //有收货地址，就查询这条信息
           AddressDO defaultAddressDO= addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("user_id",loginUser.getId()).eq("default_status",1));
           if(defaultAddressDO!=null){
             defaultAddressDO.setDefaultStatus(0);
             addressMapper.update(defaultAddressDO,new QueryWrapper<AddressDO>().eq("id",defaultAddressDO.getId()));
           }
        }

        int rows = addressMapper.insert(addressDO);
        log.info("rows{},data={}",rows,addressDO);
    }

    /**
     * 根据id查询用户地址
     * @param id
     * @return
     *   id 3用户
     *     >46  3
     *     >47  3
     *   id 4用户
     *     >48  4
     *     select * from addresss where id=46 and user_id=3
     */
    @Override
    public AddressVO findById(long id) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        AddressDO addressDO = addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("id", id).eq("user_id",loginUser.getId()));
        if(addressDO==null){
            return null;
        }
        AddressVO  addressVO=new AddressVO();

        BeanUtils.copyProperties(addressDO,addressVO);
        return addressVO;
    }

    /**
     * 删除指定的收货地址
     * @param address_id
     * @return
     */
    @Override
    public int del(long address_id) {
         LoginUser loginUser = LoginInterceptor.threadLocal.get();
         int row=addressMapper.delete(new QueryWrapper<AddressDO>().eq("id",address_id).eq("user_id",loginUser.getId()));
        return row;
    }

    /**
     * 查询所有的收货地址
     * @return
     */
    @Override
    public List<AddressVO> list() {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        List<AddressDO> addressDOList = addressMapper.selectList(new QueryWrapper<AddressDO>().eq("user_id", loginUser.getId()));
        //2条数据  46 47
        List<AddressVO> addressVOList=new ArrayList<>();

        for (int i = 0; i < addressDOList.size(); i++) {
            AddressVO addressVO=new AddressVO();
            AddressDO addressDO= addressDOList.get(i);
            BeanUtils.copyProperties(addressDO,addressVO);
            addressVOList.add(addressVO);
        }
        //Collections.copy(a,b);
        return addressVOList;
    }
}
